//
//  MLAShopFoodManageVC.m
//  MagicLampApp
//
//  Created by Zheng on 2019/4/12.
//  Copyright © 2019 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAShopFoodManageVC.h"
#import "MLAEateryFoodManageCell.h"
#import "MLAEateryFoodManageModel.h"
#import "MLALShopFoodDetailVC.h"

@interface MLAShopFoodManageVC ()

@end

@implementation MLAShopFoodManageVC

{
    NSMutableArray<MLAEateryFoodManageModel *> *modelsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configRightItemWithTitle:@"添加"];
    
    [self configTitle:@"菜品管理"];
    self.tableView.rowHeight = 120;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"MLAEateryFoodManageCell" bundle:nil] forCellReuseIdentifier:@"MLAEateryFoodManageCell"];
    self.haveRefreshHeader = YES;
    [self loadData];
}

#pragma mark - LoadData
- (void)loadData {
    [super loadData];
    
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kProductList params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self->modelsArr = [NSMutableArray array];
        self.page++;
        self.haveRefreshFooter = YES;
        self->modelsArr = [NSMutableArray array];
        self->modelsArr = [MLAEateryFoodManageModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        if (self->modelsArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        [self.tableView reloadData];
    } failure:^{
        
    }];
    
}

- (void)loadMoreData {
    NSDictionary *params = @{@"page":@(self.page)};
    [MLAHttpManage postHostType:MLAHttpHostType_Diancan api:kProductList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self.page++;
        if ([response[@"lists"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        [self->modelsArr addObjectsFromArray:[MLAEateryFoodManageModel mj_objectArrayWithKeyValuesArray:response[@"lists"]]];
        [self.tableView reloadData];
    } failure:^{
        
    }];
}


#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return modelsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLAEateryFoodManageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MLAEateryFoodManageCell" forIndexPath:indexPath];
    MLAEateryFoodManageModel *model = modelsArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MLAEateryFoodManageModel *model = modelsArr[indexPath.row];

    MLALShopFoodDetailVC *sfdVC = [[MLALShopFoodDetailVC alloc] init];
    sfdVC.productId = model.modelId;
    [self.navigationController pushViewController:sfdVC animated:YES];
    
    @weakify(self);
    sfdVC.addSuccessBlock = ^{
        @strongify(self);
        [self loadData];
    };

}

#pragma mark - Actions
- (void)rightItemAction {
    MLALShopFoodDetailVC *sfdVC = [[MLALShopFoodDetailVC alloc] init];
    [self.navigationController pushViewController:sfdVC animated:YES];
    
    @weakify(self);
    sfdVC.addSuccessBlock = ^{
        @strongify(self);
        [self loadData];
    };
}

@end
